package com.zhhs.galaxyott.activity.mainActivity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.MyApplication;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.VPInterface.IPresenter;
import com.zhhs.galaxyott.beans.HomeMovieBean;
import com.zhhs.galaxyott.http.api.RequestMaker;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.response.HomemovieListResponse;
import com.zhhs.galaxyott.httpVolley.response.IsVipResponse;
import com.zhhs.galaxyott.httpVolley.response.VipBean;
import com.zhhs.galaxyott.sharepreference.SharedPrefHelper;
import com.zhhs.galaxyott.utils.Logs;

public class MainPresenter implements IPresenter<MainHolder> {

    private Context mContext;
    private MainHolder mMainView;
    // private List<String> mlistData;
    List<HomeMovieBean> listDatas;
    List<HomeMovieBean> mlistDatas;
    List<String> RollingNewsDatas;
    // List<HomeMovieBean> listDatas;
    private SharedPrefHelper sp;

    String url = "";
    int type;

    public MainPresenter(Activity mContext) {

        this.mContext = mContext;
        sp = SharedPrefHelper.getInstance(mContext);
    }

    @Override
    public void attachView(MainHolder view) {

        this.mMainView = view;

    }

    public void loadisVipData() {
        String caId = sp.getsmartcard();
        RequestMaker.getIsVip(caId, new ResponseListener<IsVipResponse>() {

            @Override
            public void onResponse(IsVipResponse result) {

                if (result == null) {
                    return;
                }

                if (result.errCode == 0) {
                    VipBean mVipBean = new VipBean();
                    mVipBean = result.vipbean;

                    MyApplication.setVipBean(mVipBean);
                    Logs.logD("DEBUG_DT",
                            "THIS IS loadisVipData-->" + mVipBean.getResult() + "ooo" + mVipBean.getIsOnline());
                    if (mVipBean.getIsOnline().equals("1")) {
                        sp.setIsComboOnline(true);
                    } else {
                        // sp.setIsComboOnline(true);
                        sp.setIsComboOnline(false);
                    }

                    if (mVipBean.getResult() == 1) {
                        sp.setIsVip(true);
                        mMainView.vip_tag.setVisibility(View.VISIBLE);
                        mMainView.vip_tag.setImageResource(R.drawable.homepage_vip_tag);
                    } else {
                        // sp.setIsVip(true);
                        sp.setIsVip(false);
                    }

                    Logs.logD("DEBUG_DT", "THIS IS loadisVipData-->" + mVipBean.getResult());
                }

            }

            @Override
            public void onError(VolleyError error) {
                Logs.logD("DEBUG_DT", "THIS IS loadisVipData-->-1");
            }
        });
    }

    public void loadData() {
        RequestMaker.getHomeMovieList((Activity) mContext, 1, new ResponseListener<HomemovieListResponse>() {

            @Override
            public void onResponse(HomemovieListResponse result) {

                RollingNewsDatas = new ArrayList<String>();
                listDatas = new ArrayList<HomeMovieBean>();
                mlistDatas = new ArrayList<HomeMovieBean>();

                if (result.homeMovieBeans == null || result.homeMovieBeans.size() == 0) {
                    return;
                }

                for (HomeMovieBean hmbean : result.homeMovieBeans) {

                    if (hmbean.getPageModule() == 1) {
                        RollingNewsDatas.add(hmbean.getAdTitle());

                    } else if (hmbean.getPageModule() == 2) {
                        listDatas.add(hmbean);

                    } else if (hmbean.getPageModule() == 3) {
                        if (hmbean.getAdSort() == 1) {
                            Glide.with(mContext).load(hmbean.getImageUrl())
                                    .placeholder(R.drawable.homepage_rec_default).crossFade()
                                    .into(mMainView.topic_sort);
                            mMainView.setListener(1, hmbean.getAdType(), hmbean.getObjectid());
                        } else if (hmbean.getAdSort() == 2) {
                            Glide.with(mContext).load(hmbean.getImageUrl())
                                    .placeholder(R.drawable.homepage_rec_default).crossFade()
                                    .into(mMainView.activity_sort);
                            mMainView.setListener(2, hmbean.getAdType(), hmbean.getObjectid());
                        } else if (hmbean.getAdSort() == 3) {
                            Glide.with(mContext).load(hmbean.getImageUrl())
                                    .placeholder(R.drawable.homepage_rec_default).crossFade()
                                    .into(mMainView.preview_sort);
                            mMainView.setListener(3, hmbean.getAdType(), hmbean.getObjectid());
                        }

                    }

                }

                if (RollingNewsDatas.size() > 0) {
                    StringBuffer rollNewString = new StringBuffer();

                    for (String string : RollingNewsDatas) {
                        rollNewString.append(string);
                    }
                    // 显示最靠前的滚动信息
                    mMainView.push_txt.setText(RollingNewsDatas.get(0));
                    // 显示全部滚动信息
                    // mMainView.push_txt.setText(rollNewString);
                }
                if (listDatas.size() > 7) {
                    mlistDatas = listDatas.subList(0, 7);
                } else {
                    mlistDatas = listDatas;
                }

                // Logs.logD("debug", "this is-->listDatas" + mlistDatas.size()
                // + mlistDatas.get(0).getAdSort());

                if (listDatas.size() > 0) {
                    mMainView.initMovieLayout(mlistDatas.size());
                    mMainView.showData(mlistDatas);
                }

                // mMainView.initPreview(url, type);

                Logs.logD("DEBUG_DT", "THIS IS getHomeMovieList-->msg" + result.msg + "size:" + listDatas.size());

            }

            @Override
            public void onError(VolleyError error) {
                Logs.logD("DEBUG_DT", "THIS IS getHomeMovieList-->-1");

            }
        });
    }

    @Override
    public void detachView() {

        this.mMainView = null;
    }

}
